package com.ycfmedical.bed.ui;

import java.util.ArrayList;
import java.util.List;

import com.jude.rollviewpager.RollPagerView;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.base.BaseMenuActivity;
import com.ycfmedical.bed.present.ControlPresenter;
import com.ycfmedical.bed.ui.fragment.ControlCrpFragment;
import com.ycfmedical.bed.ui.fragment.ControlFreedomFragment;
import com.ycfmedical.bed.ui.fragment.ControlQuicklyFragment;
import com.ycfmedical.bed.ui.fragment.ControlnNurseFragment;
import com.ycfmedical.bed.utils.ResourceUtil;
import com.ycfmedical.bed.widgets.NoScrollViewPager;

import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;

import butterknife.BindView;
import cn.droidlover.xdroidmvp.base.XFragmentAdapter;
import cn.droidlover.xdroidmvp.router.Router;

/**
 * Created by Administrator on 2017/3/17 0017.
 */

public class ControlActivity extends BaseMenuActivity<ControlPresenter> {


    @BindView(R.id.control_tabs)
    TabLayout controlTabs;
    @BindView(R.id.control_viewpager)
    NoScrollViewPager controlViewpager;


    private String[] titles = {ResourceUtil.getContext().getString(R.string.bed_control_freedom),
            ResourceUtil.getContext().getString(R.string.bed_control_quickly),
            ResourceUtil.getContext().getString(R.string.bed_control_nurse),
            ResourceUtil.getContext().getString(R.string.bed_control_control)};

    List<Fragment> fragmentList = new ArrayList<>();
    XFragmentAdapter fragmentAdapter;

    @Override
    public void initData(Bundle savedInstanceState) {
        //禁止ViewPager手动滑动
        controlViewpager.setNoScroll(true);
        initControlViewpager();
        if(android.os.Build.VERSION.SDK_INT>9)
        {
            StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
        }
    }

    private void initControlViewpager() {
        fragmentList.clear();
        fragmentList.add(ControlFreedomFragment.newInstance());
        fragmentList.add(ControlQuicklyFragment.newInstance());
        fragmentList.add(ControlnNurseFragment.newInstance());
        fragmentList.add(ControlCrpFragment.newInstance());

        if (fragmentAdapter == null) {
            fragmentAdapter = new XFragmentAdapter(getSupportFragmentManager(), fragmentList, titles);
        }
        controlViewpager.setAdapter(fragmentAdapter);
        controlViewpager.setOffscreenPageLimit(1);
        controlTabs.setupWithViewPager(controlViewpager);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_control;
    }

    @Override
    public ControlPresenter newP() {
        return new ControlPresenter();
    }

    public static void launch(Activity activity) {
        Router.newIntent(activity).to(ControlActivity.class).launch();
    }

}
